﻿using System;
using System.Web;
using SME.Food.Surperise.BLL;

namespace SME.Food.Tricon.Web.AdminJS
{
    public partial class changepwd : System.Web.UI.Page
    {
        public string forwardUrl = "AdminJS/index.aspx";
        private Bas_User_BLL bll = new Bas_User_BLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            string userNo = "", password = "";
            if (Request.Cookies["MyCook"] != null)
            {
                userNo = Request.Cookies["MyCook"]["User_No"];
                password = Request.Cookies["MyCook"]["User_PassWord"];
            }            
            if (Request.QueryString["type"] == "change")
            {
                ChangePassword(userNo, password);
            }
        }

        public void ChangePassword(string userNo, string password)
        {
            string oldPassWord = Request.Form["oldPassword"];
            string cpnewPassWord = Request.Form["newPassword"];
            
            if (oldPassWord != password)
            {
                Response.Write("<script> href='javascript:;';alertMsg.warn('旧密码输入不正确！'); </script>");
            }

           else  if (bll.Update(cpnewPassWord, userNo))
            {
                ChangCookie(cpnewPassWord);
                Common.DwzMessage(true, true, "index", "index", forwardUrl);
               // Response.Write("<script> href='javascript:;';alertMsg.corret('操作成功！'); </script>");
            }
            else
            {
                Response.Write("<script> href='javascript:;';alertMsg.error('操作失败！'); </script>");
            }
        }

        public void ChangCookie(string cpnewPassWord)
        {
            //获取客户端的Cookie对象  
            HttpCookie cok = Request.Cookies["MyCook"];
            if (cok != null)
            {
                //修改Cookie的两种方法  
                cok.Values["User_PassWord"] = cpnewPassWord;
                //cok.Values.Set("userid", "alter-value");
                //往Cookie里加入新的内容  
                //cok.Values.Set("newid", "newValue");
                Response.AppendCookie(cok);
            }
        }
    }
}